package com.movie.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.movie.comm.utils.MD5;
import com.movie.comm.utils.PageData;
import com.movie.comm.utils.UuidUtil;
import com.movie.dao.MovieDao;
import com.movie.db.util.DBHelper;
import com.movie.entity.Movie;

public class MovieDaoImpl implements MovieDao{

	@Override
	public List<Movie> getAllMovie() {
		
		String sql = "select * from movie";
		DBHelper dbHelper = new DBHelper();
		List<Movie> movieList =null;
		try {
			movieList = dbHelper.executeQuery(sql, null, Movie.class);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return movieList;
		
	}
	
	
	//添加电影信息的业务逻辑

	@Override
	public boolean addMovie(PageData pageData) {
		String sql;
		DBHelper dbHelper;
		ArrayList<Object> params;
		String sql2;
		ArrayList<Object> imageParams;
		try {
			sql = "insert into movie values(?,?,?,?,?,?)";
			String movieId = UuidUtil.get32UUID();
			String movieName = pageData.getString("movieName");
			Integer moviePrice = Integer.parseInt(pageData.getString("moviePrice"));
			String movieInfo = pageData.getString("movieInfo");
			Integer hotLevel = Integer.parseInt(pageData.getString("hotLevel"));
			String runningTime = pageData.getString("runningTime");
			String imageURL = pageData.getString("imageURL");
			
			dbHelper = new DBHelper();
			params = new ArrayList<>();
			params.add(movieId);
			params.add(movieName);
			params.add(moviePrice);
			params.add(movieInfo);
			params.add(hotLevel);
			params.add(runningTime);
			
			String imageId = UuidUtil.get32UUID();
			sql2 = "insert into movieimages values(?,?,?)";
			imageParams = new ArrayList<>();
			imageParams.add(imageId);
			imageParams.add(imageURL);
			imageParams.add(movieId);
			return dbHelper.excuteUpdate(sql, params)&&dbHelper.excuteUpdate(sql2, imageParams);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		
	}


	/**
	 * 将查询出来的结果封装程Map
	 */
	@Override
	public List<Map<String, Object>> getAllMovieAndImage() {
		String sql = "select * from movie m ,movieimages mi where mi.movieId = m.movieId;";
		DBHelper dbHelper = new DBHelper();
		 return dbHelper.getSimpleObjs(sql, null);
	}


	@Override
	public List<Map<String, Object>> getMovieById(PageData pageData) {
		String sql = "select * from movie m ,movieimages mi where mi.movieId = m.movieId and m.movieId = ?";
		DBHelper dbHelper = new DBHelper();
		Object [] params={pageData.getString("movieId")};
		return dbHelper.getSimpleObjs(sql, params);
	}

}
